package com.lanqiao.dao;

import com.lanqiao.entity.userinfo;
import com.lanqiao.util.ORMUtil;

import java.util.List;

public class userDAO {

    ORMUtil or = new ORMUtil();

    public List<userinfo> select(String user_email, String user_pwd) {
        String sql = "select * from user where user_email = ? and user_pwd = ?";
        List<userinfo> list = or.selectList(userinfo.class, sql, user_email, user_pwd);
        return list;
    }

    public List<userinfo> selectById(String user_ID) {
        String sql = "select * from user where user_ID = ?";
        List<userinfo> list = or.selectList(userinfo.class, sql, user_ID);
        return list;
    }

    //添加用户信息
    public void inserUser(String user_name, String user_sex, String user_birth, String user_phone, String user_home, String user_type, String user_email, String user_pwd) {
        String sql = "insert into user(user_name,user_sex,user_birth,user_phone,user_home,user_type,user_email,user_pwd) values (?,?,?,?,?,?,?,?)";
        or.executeDMLByInsert(sql, user_name, user_sex, user_birth, user_phone, user_home, user_type, user_email, user_pwd);
    }

    //查询全部学生信息
    public List<userinfo> selectUser(String user_name, String user_sex, String user_phone) {
        String sql = "SELECT * FROM user ";
        List<userinfo> list;
        if (user_name.equals("")) {
            if (user_sex.equals("")) {
                if (user_phone.equals("")) {
                    list = or.selectList(userinfo.class, sql);
                } else {
                    sql += "WHERE user_phone LIKE ?";
                    list = or.selectList(userinfo.class, sql, "%" + user_phone + "%");
                }
            } else {
                if (user_phone.equals("")) {
                    sql += "WHERE user_sex LIKE ?";
                    list = or.selectList(userinfo.class, sql, "%" + user_sex + "%");
                } else {
                    sql += "WHERE user_phone LIKE ?";
                    list = or.selectList(userinfo.class, sql, "%" + user_phone + "%");
                }
            }
        } else {
            if (user_sex.equals("")) {
                if (user_phone.equals("")) {
                    sql += "WHERE user_name LIKE ?";
                    list = or.selectList(userinfo.class, sql, "%" + user_name + "%");
                } else {
                    sql += "WHERE user_name LIKE ? and user_phone LIKE ?";
                    list = or.selectList(userinfo.class, sql, "%" + user_name + "%", "%" + user_phone + "%");
                }
            } else {
                if (user_phone.equals("")) {
                    sql += "WHERE user_name LIKE ? and user_sex LIKE ?";
                    list = or.selectList(userinfo.class, sql, "%" + user_name + "%", "%" + user_sex + "%");
                } else {
                    sql += "WHERE user_name LIKE ? and user_sex LIKE ? and user_phone LIKE ?";
                    list = or.selectList(userinfo.class, sql, "%" + user_name + "%", "%" + user_sex + "%", "%" + user_phone + "%");
                }
            }
        }
        return list;
    }

}
